package com.tutu.controller.backend;

import com.tutu.common.DataTableFactory;
import com.tutu.controller.common.CommonController;
import com.tutu.core.Constant;
import com.tutu.entity.vo.BaseVo;
import com.tutu.service.AlarmRuleService;
import com.tutu.service.DeviceService;
import com.tutu.service.LogInfoService;
import com.tutu.service.UserService;
import com.tutu.utils.BaseUtil;
import com.tutu.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人员管理
 */
@Controller
@RequestMapping(value = "backend/user")
public class UserController extends CommonController {

    public static Map<String, String> tempUserCompanyMap = new HashMap<>();

    @Autowired
    UserService userService;

    @Autowired
    DeviceService deviceService;

    @Autowired
    AlarmRuleService alarmRuleService;

    @Autowired
    LogInfoService logInfoService;

    @RequestMapping(value = "/index")
    public String index() {
        return "backend/user/list";
    }

    @RequestMapping(value = "/child/index")
    public String childIndex(Model model, Integer id, Integer pageNum) {
        model.addAttribute("user", userService.getInfoById(id));
        model.addAttribute("pageNum", pageNum);

        return "backend/user/childList";
    }

    @RequestMapping(value = "/alarmRule/index")
    public String alarmRuleIndex(Model model, String deviceCode, Integer userId, Integer pageNum) {
        model.addAttribute("device", deviceService.getInfoByCode(deviceCode));
        model.addAttribute("user", userService.getInfoById(userId));
        model.addAttribute("pageNum", pageNum);

        return "backend/user/alarmRuleList";
    }

    @RequestMapping("/list")
    public void list(HttpServletRequest request, HttpServletResponse response, Integer searchCompanyId, String keyWord, Integer draw, Integer start, Integer length) {
        int pageNum = getPageNum(start, length);
        String companyId = request.getSession().getAttribute(Constant.CURRENT_LOGIN_COMPANY_ID) + "";
        if (null != searchCompanyId) companyId = searchCompanyId + "";

        Map<String, Object> result = DataTableFactory.fitting(draw, userService.pageByParams(BaseUtil.isBlank(companyId) ? null : Integer.parseInt(companyId), keyWord, pageNum, length));

        WebUtil.printJson(response, result);
    }

    @RequestMapping("/child/list")
    public void childList(HttpServletRequest request, HttpServletResponse response, Integer userId, Integer draw, Integer start, Integer length) {
        int pageNum = getPageNum(start, length);
        String companyId = request.getSession().getAttribute(Constant.CURRENT_LOGIN_COMPANY_ID) + "";

        Map<String, Object> result = DataTableFactory.fitting(draw, userService.pageByChildParams(BaseUtil.isBlank(companyId) ? null : Integer.parseInt(companyId), userId, pageNum, length));

        WebUtil.printJson(response, result);
    }

    @RequestMapping("/alarmRuleList")
    public void alarmRuleList(HttpServletResponse response, String deviceCode, Integer type, Integer userId, String keyWord, Integer draw, Integer start, Integer length) {
        int pageNum = getPageNum(start, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, alarmRuleService.pageByParams(deviceCode, type, userId, keyWord, pageNum, length));

        WebUtil.printJson(response, result);
    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> save(HttpServletRequest request, Integer id, String openId, String unionId, String nickName, String name, String mobile, String avatar, String description) {
        return userService.saveInfo(request, id, openId, unionId, nickName, name, mobile, avatar, description);
    }

    @RequestMapping("/del")
    @ResponseBody
    public Map<String, Object> del(HttpServletRequest request, Integer id) {
        return userService.deleteById(request, id);
    }

    @RequestMapping("/batchDel")
    @ResponseBody
    public Map<String, Object> batchDel(HttpServletRequest request, String ids) {
        return userService.batchDel(request, ids);
    }

    @RequestMapping("/baseList")
    @ResponseBody
    public List<BaseVo> baseList() {
        return userService.findBaseList();
    }

    @RequestMapping("/findListByCompanyId")
    @ResponseBody
    public List<BaseVo> findListByCompanyId(Integer companyId) {
        return userService.findListByCompanyId(companyId);
    }

    @RequestMapping("/findUserListWithCompanyUser")
    @ResponseBody
    public List<Integer> findUserListWithCompanyUser(Integer companyId) {
        return userService.findUserListWithCompanyUser(companyId);
    }

    @RequestMapping("/sync")
    @ResponseBody
    public Map<String, Object> sync(HttpServletRequest request) {
        return userService.syncData(request);
    }

    @RequestMapping("/updateDeviceUser")
    @ResponseBody
    public Map<String, Object> updateDeviceUser(HttpServletRequest request, String deviceCode, Integer userId, Integer type, Integer flag) {
        return userService.updateDeviceUser(request, deviceCode, userId, type, flag);
    }

    @RequestMapping("/changeRelation")
    @ResponseBody
    public Map<String, Object> changeRelation(HttpServletRequest request, Integer alarmRuleId, Integer userId, Integer flag) {
        return userService.changeRelation(request, alarmRuleId, userId, flag);
    }

    @RequestMapping("/noticeRefreshUserInfo")
    @ResponseBody
    public Integer noticeRefreshUserInfo() {
        return 1;
    }
}
